﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Component.WorkPool;
using RooHui.Infrastructure.Component.WorkPool.WorkHandle.ShortMessage;
using RooHui.Infrastructure.Configuration;

namespace RooHui.Portal.Biz
{
    public class SupportBiz
    {
        private static string GetRandomCode(int length)
        {
            StringBuilder sb = new StringBuilder();
            var random = new Random();
            for (int i = 0; i < length; i++)
            {
                sb.Append(random.Next(0, 9).ToString());
            }
            return sb.ToString();
        }
        public static CheckCodeStream GetCheckCode(string username, CheckCodeType checkCodeType, int codeLength = 6, bool isRefrash = true)
        {
            var code = new CheckCodeStream().Find(c => c.UserName == username
             && c.Type == (int)checkCodeType);
            if (!isRefrash)
            {
                return code;
            }

            var newcode = GetRandomCode(codeLength);
            if (code == null)
            {
                //第一次获取此类型验证码
                //添加此类型验证码
                code = new CheckCodeStream()
                {
                    Code = newcode,
                    ExpireDate = DateTime.Now.Add(new TimeSpan(0, 5, 0)),
                    RecordDate = DateTime.Now,
                    Type = checkCodeType,
                    UserName = username
                }.PostAdd();
            }
            else
            {
                code.Code = newcode;
                code.ExpireDate = DateTime.Now.Add(new TimeSpan(0, 5, 0));
                code.RecordDate = DateTime.Now;
                code = code.PostModify();
                //覆盖之前验证码
            }
            return code;
        }
        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="mobile"></param>
        /// <returns>返回正确的验证码</returns>
        public static string SendShortMessageCode(string username, string mobile,CheckCodeType checkCodeType)
        {
            var entity = GetCheckCode(username, checkCodeType);
            WorkPool.Append<NormalMessage>(WorkType.shortmessage_for_normal,
                        new NormalMessage()
                        {
                            Message = String.Format("您的{0}短信验证码为：{1}，请勿泄漏此验证码，{0}工作人员不会向您询问短信验证码。",
                                SiteSettings.SiteName, entity.Code),
                            Recipient = mobile
                        });
            return entity.Code;
        }

        public static bool CheckShortMessageCode(string username, string mobileCode, CheckCodeType checkCodeType)
        {
            var entity = GetCheckCode(username, checkCodeType);
            if (entity == null || entity.ExpireDate < DateTime.Now || entity.Code != mobileCode)
            {
                return false;
            }
            return true;
        }
    }
}
